package com.kj.xuyuan.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.DynamicAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.DynamicBean;
import com.kj.xuyuan.util.GlideUtils;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.PhotoViewActivity;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 动态话题详情
 */
public class DynamicTopicDetailsActivity extends BaseActivity {


    @BindView(R.id.iv_top_bg)
    ImageView ivTopBg;
    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_topic_content)
    TextView tvTopicContent;
    @BindView(R.id.tv_topic)
    TextView tvTopic;
    @BindView(R.id.rv_topic)
    RecyclerView rvTopic;
    @BindView(R.id.tv_join_topic)
    TextView tvJoinTopic;
    @BindView(R.id.sml_topic)
    SmartRefreshLayout smlTopic;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int topicId;
    private int page = 1;
    private String content,image,title;
    private List<DynamicBean> dynamicList;
    private DynamicAdapter dynamicAdapter;

    public static void actionStar(Context context,int topicId,String title,String content,String image){
        Intent intent = new Intent(context,DynamicTopicDetailsActivity.class);
        intent.putExtra("topicId",topicId);
        intent.putExtra("content",content);
        intent.putExtra("image",image);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_dynamic_topic_details;
    }

    @Override
    protected void initView() {
        topicId = getIntent().getIntExtra("topicId",-1);
        content = getIntent().getStringExtra("content");
        image = getIntent().getStringExtra("image");
        title = getIntent().getStringExtra("title");
        if(topicId == -1){
            ToastUtil.showDataErrorToast(context);
            finish();
            return;
        }

        tvTopicContent.setText(content);
        tvTopic.setText(title);
        GlideUtils.loadRoundImage(context,ivImage,UrlConstants.Base_Http+image,8,R.mipmap.ic_error);
        GlideUtils.loadRoundImage(context,ivTopBg,UrlConstants.Base_Http+image,8,R.mipmap.ic_error);
        initRecyclerView();
        getDynamic();
    }

    private void initRecyclerView() {
        dynamicList = new ArrayList<>();
        dynamicAdapter = new DynamicAdapter();
        rvTopic.setLayoutManager(new LinearLayoutManager(getContext()));
        rvTopic.setAdapter(dynamicAdapter);
        dynamicAdapter.setNewData(dynamicList);

        smlTopic.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getDynamic();
            }
        });

        smlTopic.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page ++;
                getDynamic();
            }
        });

        dynamicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DynamicDetailsActivity.actionStar(getContext(),dynamicList.get(position).getId());
            }
        });

        dynamicAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_like_or_chat:
                        //是否点过喜欢 0否 1是
                        if(dynamicList.get(position).getIs_like_message() == 0){//没点过，走喜欢逻辑
                            like(position);
                        }else{//点过，跳转聊天页

                        }
                        break;
                    case R.id.tv_like:
                        dynamicLike(position);
                        break;
                    case R.id.tv_comment:
                        DynamicDetailsActivity.actionStar(getContext(),dynamicList.get(position).getId());
                        break;
                    case R.id.tv_tag:
                        DynamicTopicDetailsActivity.actionStar(getContext(),dynamicList.get(position).getTopic_id(),
                                dynamicList.get(position).getTopic().getTitle(),
                                dynamicList.get(position).getTopic().getContent(),
                                dynamicList.get(position).getTopic().getCover_image());
                        break;
                }
            }
        });

        dynamicAdapter.setImageClickListener(new DynamicAdapter.ImageClickListener() {
            @Override
            public void imageClick(int position1, int position2) {
                LogUtils.e(TAG,"54654563");
                PhotoViewActivity.actionStar(getContext(),dynamicList.get(position1).getImage(),position2);
            }
        });
    }

    private void dynamicLike(int position) {
        showDialog();
        Map map = new HashMap();
        //是否点赞过本条动态 0否 1是
        map.put("type",dynamicList.get(position).getIs_like() == 0 ? "1" : "0");
        map.put("topic_comment_id",dynamicList.get(position).getId()+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.DYNAMIC_LIKE, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        dynamicList.get(position).setIs_like(dynamicList.get(position).getIs_like() == 0 ? 1 : 0);
                        dynamicAdapter.notifyDataSetChanged();
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                dismissDialog();
            }
        });
    }

    private void like(int position) {
        showDialog();
        Map map = new HashMap();
        map.put("for_user_id",dynamicList.get(position).getUser().getId()+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.LIKE, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        dynamicList.get(position).setIs_like_message(dynamicList.get(position).getIs_like_message() == 0 ? 1 : 0);
                        dynamicAdapter.notifyDataSetChanged();
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                dismissDialog();
            }
        });
    }

    private void getDynamic() {
        Map map = new HashMap();
        map.put("page", page+"");
        map.put("id", topicId+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.DYNAMIC_LIST, map, new JsonCallback<BaseModel<List<DynamicBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<DynamicBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        if(page == 1){
                            dynamicList.clear();
                        }
                        dynamicList.addAll(response.body().getData());
                        dynamicAdapter.notifyDataSetChanged();

                        llNoData.setVisibility(dynamicList.size() == 0 ? View.VISIBLE : View.GONE);
                        rvTopic.setVisibility(dynamicList.size() == 0 ? View.GONE : View.VISIBLE);
                    }else{
                        ToastUtil.showToast(getContext(),response.body().getMsg());
                    }
                }
                smlTopic.finishRefresh();
                smlTopic.finishLoadMore();
            }

            @Override
            public void onError(Response<BaseModel<List<DynamicBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                smlTopic.finishRefresh();
                smlTopic.finishLoadMore();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    @OnClick({R.id.iv_back,R.id.tv_join_topic})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_join_topic:
                PublishDynamicActivity.actionStar(getContext(),topicId,title);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}